﻿using Share.UI;
using TableDef;
using UnityEngine;
namespace Share
{
    /// <summary>
    /// UI面板基类
    /// </summary>
    public class PanelBase : EventReciever
    {

        public UICore Parent { get; set; }
        public t_windowdef ResData { get; set; }
        [SerializeField]
        [Header("延迟关闭时间")]
        float m_fDelayTime = 0;

        private string m_strPanelName;

        //所在互斥组
        public long UIGroup
        {
            get { return ResData.m_nToggleGroup; }
        }
        // 是否为逻辑窗口，逻辑窗口不存在onTop逻辑。
        public bool IsLogicWindow
        {
            get { return ResData.m_nLogicWindow > 0; }
        }

        public string PanelName
        {
            get { return m_strPanelName; }
            set { m_strPanelName = value; }
        }

        protected Canvas m_Canvas = null;
        public Canvas Canvas
        {
            get { return m_Canvas; }
            set { m_Canvas = value; }
        }

        private int m_SortOrder = 0;
        public int SortOrder
        {
            get { return m_SortOrder; }
            set
            {
                m_SortOrder = value;
                if (m_Canvas != null)
                {
                    m_Canvas.overrideSorting = true;
                    m_Canvas.sortingOrder = value;
                }
            }
        }

        public bool IsShown { get; set; }
        public bool IsCover { get; set; }

        /// <summary>
        /// 处理返回操作
        /// </summary>
        public virtual void DoEscape() { }

        protected override bool OnEscape(int ge, EventParam parameter)
        {
            if (IsFrontWindow() && IsLogicWindow)
                DoEscape();
            return true;
        }

        protected virtual void OnParam(EventParam param)
        {
        }
        public virtual void OnTopModify()
        {
            if (IsShown && IsCover == false)
                DoPlayBGM();
        }
        public void DoDestory()
        {
            gameObject.StopOrDestroy();
        }
        /// <summary>
        /// 关闭面板
        /// </summary>
        public bool DoClose()
        {
            if (ResData.m_nRealClose > 0)
            {
                Parent.DoCloseWindow(this);
                return true;
            }
            else
                DoHide();
            return false;
        }

        public void DoCloseAction()
        {
            Invoke(nameof(DoClose), m_fDelayTime);
        }


        public void DoPlayBGM()
        {
            if (ResData.m_strBgm != "")
                AudioManager.Instance.PlayBgm($"Music/BGM/{ResData.m_strBgm}");
        }
        /// <summary>
        /// 显示面板
        /// </summary>
        public bool DoShow(EventParam param = null)
        {

            IsShown = true;
            SetActive(IsShown);
            OnParam(param);
            UIMgr.Instance.DoFreshSortWindow();
            return true;
        }

        /// <summary>
        /// 隐藏面板
        /// </summary>
        public bool DoHide()
        {
            IsShown = false;
            SetActive(IsShown);
            UIMgr.Instance.DoFreshSortWindow();
            AudioManager.Instance.StopBgm();
            return false;
        }

        /// <summary>
        /// 被遮挡
        /// </summary>
        /// <returns></returns>
        public bool DoBeCover()
        {
            IsCover = true;
            SetActive(false);
            AudioManager.Instance.StopBgm();
            return true;
        }

        // <summary>
        /// 置顶
        /// </summary>
        public void DoTop()
        {
            IsCover = false;
            SetActive(true);
            OnTopModify();
        }

        /// <summary>
        /// 设置启用状态
        /// </summary>
        public void SetActive(bool isEnable)
        {
            gameObject.SetActive(isEnable);
            //if (m_Canvas != null)
            //{
            //    m_Canvas.enabled = isEnable;
            //}
        }

        /// <summary>
        /// 重置面板层级
        /// </summary>
        public void DoResetOrder(int nOrder)
        {
            if (Canvas != null)
            {
                SortOrder = nOrder;
                Canvas.planeDistance = 100;
            }

            var so = GetComponentsInChildren<UISortingOrderBase>(true);
            foreach (var item in so)
            {
                item.SetOrder(nOrder);
            }

            var pathRenders = GetComponentsInChildren<LineRenderer>(true);
            foreach (var render in pathRenders)
            {
                render.sortingOrder = nOrder + 1;
            }
        }


        /// <summary>
        /// 判断是否为最前端窗口
        /// </summary>
        public bool IsFrontWindow()
        {
            return UIMgr.Instance.GetTopPanel() == this;
        }

        public bool IsFullWindow()
        {
            return ResData.m_nFullWindow > 0;
        }

        protected void OnStartShow()
        {
            var effects = gameObject.GetComponentsInChildren<UIEffectBase>();
            foreach (var item in effects)
            {
                item.DoActionEnter();
            }

        }
        protected void OnCloseShow()
        {
            var effects = gameObject.GetComponentsInChildren<UIEffectBase>();
            foreach (var item in effects)
            {
                item.DoActionExit();
            }

        }
    }
}